{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Generate Reactions\n",
    "\n",
    "This script performs the same task as the script in `scripts/generateReactions.py` but in visual ipynb format.\n",
    "It can also evaluate the reaction forward and reverse rates at a user selected temperature."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from rmgpy.rmg.main import RMG\n",
    "from rmgpy.rmg.model import CoreEdgeReactionModel\n",
    "from rmgpy import settings\n",
    "from IPython.display import display\n",
    "from arkane.output import prettify"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Declare database variables here by changing the thermo and reaction libraries, or restrict to certain reaction families.  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "database = \"\"\"\n",
    "database(\n",
    "    thermoLibraries = ['BurkeH2O2','primaryThermoLibrary','DFT_QCI_thermo','CBS_QB3_1dHR','Narayanaswamy','Chernov'],\t\t\n",
    "    reactionLibraries = [],\n",
    "    seedMechanisms = [],\n",
    "    kineticsDepositories = ['training'],\n",
    "    kineticsFamilies = [\n",
    "        'H_Abstraction',\n",
    "        'R_Addition_MultipleBond',\n",
    "        'intra_H_migration',\n",
    "        'Intra_R_Add_Endocyclic',\n",
    "        'Intra_R_Add_Exocyclic'\n",
    "    ],\n",
    "    kineticsEstimator = 'rate rules',\n",
    ")\n",
    "\n",
    "options(\n",
    "    verboseComments=True,  # Set to True for detailed kinetics comments\n",
    ")\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "List all species you want reactions between"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "species_list = \"\"\"\n",
    "species(\n",
    "    label='i1',\n",
    "    reactive=True,\n",
    "    structure=adjacencyList(\n",
    "        \\\"\"\"\n",
    "        multiplicity 2\n",
    "        1 C u0 p0 c0 {3,S} {4,S} {10,S} {11,S}\n",
    "        2 C u0 p0 c0 {4,S} {12,S} {13,S} {14,S}\n",
    "        3 C u0 p0 c0 {1,S} {5,B} {6,B}\n",
    "        4 C u1 p0 c0 {1,S} {2,S} {15,S}\n",
    "        5 C u0 p0 c0 {3,B} {8,B} {19,S}\n",
    "        6 C u0 p0 c0 {3,B} {9,B} {20,S}\n",
    "        7 C u0 p0 c0 {8,B} {9,B} {17,S}\n",
    "        8 C u0 p0 c0 {5,B} {7,B} {16,S}\n",
    "        9 C u0 p0 c0 {6,B} {7,B} {18,S}\n",
    "        10 H u0 p0 c0 {1,S}\n",
    "        11 H u0 p0 c0 {1,S}\n",
    "        12 H u0 p0 c0 {2,S}\n",
    "        13 H u0 p0 c0 {2,S}\n",
    "        14 H u0 p0 c0 {2,S}\n",
    "        15 H u0 p0 c0 {4,S}\n",
    "        16 H u0 p0 c0 {8,S}\n",
    "        17 H u0 p0 c0 {7,S}\n",
    "        18 H u0 p0 c0 {9,S}\n",
    "        19 H u0 p0 c0 {5,S}\n",
    "        20 H u0 p0 c0 {6,S}\n",
    "        \\\"\"\"\n",
    "    )\n",
    ")\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write input file to disk\n",
    "with open('temp/input.py','w') as input_file:\n",
    "    input_file.write(database)\n",
    "    input_file.write(species_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# Execute generate reactions\n",
    "from rmgpy.tools.generate_reactions import RMG, execute\n",
    "kwargs = {\n",
    "    'walltime': '00:00:00:00',\n",
    "    'kineticsdatastore': True\n",
    "}\n",
    "rmg = RMG(input_file='temp/input.py', output_directory='temp')\n",
    "rmg = execute(rmg, **kwargs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Pick some temperature to evaluate the forward and reverse kinetics\n",
    "T = 623.0 # K"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for rxn in rmg.reaction_model.output_reaction_list:\n",
    "    print('=========================')\n",
    "    display(rxn)\n",
    "    print('Reaction Family = {0}'.format(rxn.family))\n",
    "    print('')\n",
    "    print('Reactants')\n",
    "    for reactant in rxn.reactants:\n",
    "        print('Label: {0}'.format(reactant.label))\n",
    "        print('SMILES: {0}'.format(reactant.molecule[0].to_smiles()))\n",
    "        print('')\n",
    "    print('Products')\n",
    "    for product in rxn.products:\n",
    "        print('Label: {0}'.format(product.label))\n",
    "        print('SMILES: {0}'.format(product.molecule[0].to_smiles()))\n",
    "    print('')\n",
    "    print(rxn.to_chemkin())\n",
    "    print('')\n",
    "    print('Heat of Reaction = {0:.2F} kcal/mol'.format(rxn.get_enthalpy_of_reaction(623.0)/4184))\n",
    "    print('Forward kinetics at {0} K: {1:.2E}'.format(T, rxn.get_rate_coefficient(T)))\n",
    "\n",
    "    reverseRate = rxn.generate_reverse_rate_coefficient()\n",
    "    print('Reverse kinetics at {0} K: {1:.2E}'.format(T, reverseRate.get_rate_coefficient(T)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [conda env:rmg_env]",
   "language": "python",
   "name": "conda-env-rmg_env-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
